package cn.xeblog.plugin.setting;

import cn.xeblog.commons.constants.Commons;
import cn.xeblog.plugin.persistence.PersistenceService;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.util.NlsContexts;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * 插件配置设置
 *
 * @author anlingyi
 * @date 2022/7/23 5:58 PM
 */
public class PluginConfigSetting implements SearchableConfigurable {

    /**
     * token输入框
     */
    private JPasswordField tokenInput;

    @Override
    public @NotNull
    @NonNls String getId() {
        // 插件标识
        return Commons.KEY_PREFIX + "setting";
    }

    @Override
    public @NlsContexts.ConfigurableName String getDisplayName() {
        // 可配置组件的可见名称
        return null;
    }

    @Override
    public @Nullable JComponent createComponent() {
        // 配置面板
        JPanel configPanel = new JPanel();
        configPanel.setLayout(null);

        // token文本
        JLabel tokenLabel = new JLabel("Token:");
        tokenLabel.setBounds(10, 0, 50, 30);
        configPanel.add(tokenLabel);

        // token输入框
        tokenInput = new JPasswordField();
        tokenInput.setBounds(60, 0, 300, 30);
        tokenInput.setText(PersistenceService.getData().getToken());
        configPanel.add(tokenInput);

        return configPanel;
    }

    @Override
    public boolean isModified() {
        // 可以修改
        return true;
    }

    @Override
    public void apply() throws ConfigurationException {
        if (tokenInput != null) {
            // 应用时将数据持久化
            PersistenceService.getData().setToken(tokenInput.getText());
        }
    }

}
